﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autofac;
using PkwDevFramework.Common;
using PkwDevFramework.WpfMvvm;
using PkwHCFramework.ViewModels.Pages;
using SqlSugar;

namespace PkwHCFramework.ViewModels
{
    /// <summary>
    /// VM定位器
    /// </summary>
    public class ViewModelLocator
    {
        public ViewModelLocator()
        {
            //实现依赖注入，IOC容器创建
            ContainerBuilder builder = new ContainerBuilder();
            #region 数据库注册

            //数据库注册
            builder.Register<ISqlSugarClient>(context =>
            {
                var connectionString =
                    "Data Source=127.0.0.1;Database=hongfeng;User ID=root;Password=123456;CharSet=utf8;port=3306;";
                SqlSugarClient db = new SqlSugarClient(
                    new ConnectionConfig()
                    {
                        ConnectionString = connectionString,
                        DbType = DbType.MySql,
                        IsAutoCloseConnection = true,
                    }
                );
                db.Aop.OnLogExecuted = (sql, par) => {
                    //Console.WriteLine();
                    //Console.WriteLine($"{DateTime.Now.ToString()}--sql语句：{sql}");
                    //Console.WriteLine("-------------------------------------");
                };
                return db;
            });
            //注册仓储
            builder.RegisterGeneric(typeof(Repository<>));
            #endregion

            //注册VM
            builder.RegisterType<MainViewModel>();
            builder.RegisterType<HomeViewModel>();

            //创建Ioc容器
            IocContainer.SetContainer(builder.Build());
        }

        #region ViewModel实例


        public MainViewModel Main
        {
            get { return IocContainer.Resolve<MainViewModel>(); }
        }

        public HomeViewModel Home
        {
            get { return IocContainer.Resolve<HomeViewModel>(); }
        }

        #endregion
    }
}
